<table width="100%" class="tblSort" border="1" cellspacing="0" rules="all" cellpadding="0">
<thead>
  <tr>
    <th>F.Proceso</th>
    <th>F.Documento</th>
    <th>Documento</th>
    <th>Articulo</th>
    <th>Almacen</th>
    <th>Cantidad</th>
    <th>UM</th>
    <th>CF</th>
    <th>P.Unitario</th>
    <th>CI</th>
    <th>Neto</th>
    <th>Usuario</th>
    <th>Referencia</th>
  </tr>
  </thead>
  <tbody>
  <?php

include('../../Connections/KRAPPERCON.php');
$codart = getVal('t_kx_codart');
$alm = getVal('cbo_kx_alm');
$fini = getVal('t_kx_fini');
$ffin = getVal('t_kx_ffin');
$clase = getVal('cbo_kx_clase');
$ref = getVal('referencia');
$and = '';

$tot_qt = 0;

$wh_cod = '';
$wh_alm = '';
$wh_fini1 = '';
$wh_fini2 = '';
$wh_ffin1 = '';
$wh_ffin2 = '';
$wh_fini3 = '';
$wh_ffin3 = '';
$wh_clad = '';
$wh_ref = '';

$wh_clad = isNull($clase,$and. " a.ClaDocID = '".$clase."'");

if (!isEmpty($wh_clad)) $and = ' and ';
$wh_ref = isNull($ref,$and." a.MovimientoRef = '".$ref."'");

if (!isEmpty($wh_ref)) $and = ' and ';
$wh_cod = isNull($codart,$and." b.ArticuloID = '".$codart."'");

if (!isEmpty($wh_cod)) $and = ' and ';
$wh_alm = isZeros($alm,$and." b.AlmacenID = ".$alm);



if (isEmpty($ffin)) {
	if (!isEmpty($wh_alm.$wh_cod)) $and = ' and ';
	
	$wh_fini1 = isZeros($fini,$and." date(a.MovimientoFec) = '".$fini."'");
	/* $wh_fini3 = isZeros($alm," a. = ".$alm); */
	}
else {
	if (!isEmpty($wh_cod.$wh_alm)) $and = ' and ';	
	
	$wh_fini1 = isZeros($fini,$and." date(a.MovimientoFec) >= '".$fini."'");

	
	if (!isEmpty($wh_fini1.$wh_cod.$wh_alm)) $and = ' and ';
	
	$wh_ffin1 = isZeros($ffin,$and." date(a.MovimientoFec) <= '".$ffin."'");
	
}
$where = '';
//$wcladoc = ' ';


if (!isEmpty($wh_cod . $wh_alm . $wh_fini1 . $wh_ffin1. $wh_clad. $wh_ref)) 
$where = ' where ';

$sql_prev = "select a.ClaDocID,a.MovimientoFec as feccre, a.MovimientoFeD  as fecdoc,a.MovimientoID docum,a.MovimientoTxt texto,b.ArticuloID articulo,
b.AlmacenID almacen, b.MovimientoQty ctd, b.UnMedida unm,b.ArticuloCst pru,b.MovimCstInd cstind, b.CtdFinal ctdf, b.Signo, 
(b.ArticuloCst + b.MovimCstInd) * b.MovimientoQty neto,
a.MovimientoRef ref,a.UserID usuario from tt_movimiento as a inner join tt_detmovimiento as b
on a.MovimientoID = b.MovimientoID  ";

$sql = $sql_prev. $where .$wh_clad. $wh_ref. $wh_cod . $wh_alm . $wh_fini1 . $wh_ffin1 ;
/*
$sql_in = "select a.FactCompFeCr as feccre,a.FactCompFeFc as fecdoc,a.FactCompID as docum,b.FactCompPos as posicion,FactCompTexto as texto,ArticuloID as articulo,AlmacenID AS almacen,FactCompQta as ctd,FactCompPrun as pru,FactCompNeto as neto,FacturaRef as ref from tt_facturacomp as a inner join tt_factcompdet as b on a.FactCompId = b.FactCompId ". $where . $wh_cod . $wh_alm . $wh_fini1 . $wh_ffin1 ;
 
$sql_ajus = "select a.MovimientoFec feccre, a.MovimientoFec fecdoc, a.MovimientoID docum,b.MovimientoPos as posicion,MovimientoTxt as  
texto , b.ArticuloID as articulo,b.AlmacenID almacen, MovimientoQty ctd, ArticuloCst pru,ArticuloCst*MovimientoQty neto,MovimientoRef ref
from tt_movimiento as a inner join tt_detmovimiento as b
on a.MovimientoID = b.MovimientoID " .$where. $wh_cod . $wh_alm . $wh_fini3 . $wh_ffin3 ;

$sql_out = "select FactFecCre as feccre,FactFecFac as fecdoc,a.FacturaId as docum,DetFacPos as posicion,' ' as texto,ArticuloID as articulo,AlmacenID as almacen,DetFacQty*-1 as ctd,cast(DetFactCst/DetFacQty as decimal(10,2))* -1 as pru,DetFactCst as neto,FactFolio as ref from vt_facturavta as a inner join tt_detfacturavta as b on a.FacturaID = b.FacturaID " .$where. $wh_cod . $wh_alm . $wh_fini2 . $wh_ffin2 ;
*/
$movim = getVal('movimiento');
$movdoc = '';
if (isset($_GET['movimiento'])){
$movim = getGet('movimiento');
}
$movref = getVal('referencia');
$do_mov = false;
$do_ref = false;

if ($movim!=''){
$do_mov = true;
$movref = '';
}

if ($movref!=''){
$do_ref = true;
$movim = '';
}
$feccre = '';
$fedo = '';
$usrid = '';
$ref = '';

$buscar = ($do_mov || $do_ref);

if ($buscar){
	//$movim = getVal('movimiento');
	$clamov = '';
	if ($buscar){
	$sql1 = "call sp_getMovimientoCab('".$movim."','".$movref."');";
	$res = $mysqli->exeQuery($sql1);
	if ($mysqli->affected_rows == 1){
	$row = $res->fetch_assoc();
	$movim = $row['MovimientoID'];
	$clamov = $row['ClaDocID'];
	$fedo = $row['MovimientoFeD'];
	$movdoc = $row['MovimientoDoc'];
	$usrid = $row['UserID'];
	$txt = $row['MovimientoTxt'];
	$feccre = $row['MovimientoFec'];
	$ref = $row['MovimientoRef'];
	if ($ref == ''){
	$btn = 'Generar Guia';
	$tit = '';
	}
	else {
	$btn = $ref;
	$tit = $ref;
	}
	?>
    <tr>
    <td colspan="13">
    <div style="margin:10px;">
    <div class="subTit">
    <?php 
    echo "Movimiento de Mercancias ".$movim." creado por ".$usrid." el ".$feccre;	
    ?>
    </div>
    <?php 
	// Si es traslado, es posible generar Guia
if ($clamov == 'MV_TR' || $clamov == 'VT_SM' || $clamov == 'MV_SM'){
	$partida = substr($row['AlmacenID1'],0,1)."000";
	$destino = substr($row['AlmacenID2'],0,1)."000";
	?>
    Referencia : &nbsp;<input type="button" onclick="doGuiaMigo(this,'<?php echo $partida;?>','<?php echo $destino;?>','<?php echo $clamov;?>')" title="<?php echo $tit; ?>" texto="<?php echo $movim;?>"  value="<?php echo $btn;?>" /><br />
    Texto : &nbsp;<?php echo $txt;?>
    <table width="100%" style="padding:0px;margin:0px"><tr></tr></table>
    <?php 
}
	?>
    </div>    </td>
    </tr>
    <?php 
	
	$sql = "";
	
	if ($clamov == 'VT_TM'){

	$sql = "select sign(DetFacQty*-1) Signo,'".$feccre."' feccre,'".$fedo."' fecdoc,'".$movim."' docum,ArticuloID articulo,AlmacenID almacen,DetFacQty ctd,DetFactCst pru,DetFactCst*DetFacQty neto,DetFacQty ctdf ,0 cstind,DetFactUM unm,'".$usrid."' usuario,'".$ref."' ref from tt_detfacturavta where FacturaID = '".$movdoc."';";
	
	}
	else {
	$sql = $sql_prev.' where a.MovimientoID = '.$movim;
	}
	
	
	$res->close();
	$mysqli->check_result();
	}
	else {
	?>
    <script type="text/javascript">
	showError('Nro de Movimiento no existe'); closePopup();
    </script>
    <?php 	
	exit;
	}	
		

	}	
	
//$sql = $sql.' order by feccre, articulo';

}

doEcho($sql);
//$sql = "call sp_queryKardex('".$codart."','".$alm."','".$fini."','".$ffin."',".$clase.")";
$res = $mysqli->query($sql) or die($mysqli->error.__LINE__);;

if($res){
	while($row = $res->fetch_assoc()){	
?>
  <tr class='<?php if ($row['Signo']==1) echo 'clsIn'; else echo 'clsOut'; ?>'>
    <td><?php echo $row['feccre']; ?></td>
    <td><?php echo $row['fecdoc']; ?></td>
    <td><?php echo $row['docum']; ?></td>
    <td><?php echo $row['articulo']; ?></td>
    <td><?php echo $row['almacen']; ?></td>
    <td class="qt"><?php echo $row['ctd']; ?></td>
    <td ><?php echo $row['unm'];?></td>
    <td class="qt"><?php echo $row['ctdf'];?></td>
    <td class="qt"><?php echo $row['pru']; ?></td>
    <td class="qt"><?php echo $row['cstind'];?></td>
    <td class="qt"><?php echo $row['neto']; ?></td>
    <td><?php echo $row['usuario'];?></td>
    <td><?php echo $row['ref']; ?></td>
  </tr>
  <?php   	
	$tot_qt +=  $row['ctdf'] * $row['Signo']; 
	}	
	$res->close();
	
	$mysqli->check_result();
	
}
  ?>
  </tbody>
  <tfoot>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <th class="qt">&nbsp;</th>
    <td>&nbsp;</td>
    <td class="qt"><span ><?php echo rnd($tot_qt);?></span></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  </tfoot>
</table>
